package ex.hql.controller;

import ex.hql.config.FastDFSUtils;
import ex.hql.pojo.Admin;
import ex.hql.pojo.RespBean;
import ex.hql.service.IAdminService;
import ex.hql.utils.AdminUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @program:yeb
 * @author: hql
 * @Time: 2021/9/13  17:04
 * @description: 个人中心信息
 */
@RestController
@RequestMapping("/system")
public class AdminInfoController {

    @Autowired
    private IAdminService adminService;

    @ApiOperation(value = "更新当前用户信息")
    @PutMapping("/admin/info")
    public RespBean updateAdmin(@RequestBody Admin admin, Authentication authentication){
        if(adminService.updateById(admin)){
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,null,authentication.getAuthorities()));
            return RespBean.success("更新用户信息成功！");
        }else {
            return RespBean.error("更新用户信息失败!");
        }
    }

    @ApiOperation(value = "更新密码")
    @PutMapping("/admin/pass")
    public RespBean updatePassWord(@RequestBody Map<String , Object> info){
        String oldPass = (String) info.get("oldPass");
        String pass = (String) info.get("pass");
        Integer id = AdminUtils.getCurrentAdmin().getId();
        return adminService.updateAdminPassword(oldPass,pass,id);
    }

    @ApiOperation(value = "更新用户头像")
    @PostMapping("/admin/userface")
    public RespBean updateAdminUserFace(MultipartFile file,Integer id,Authentication authentication){
        //上传之后路径
        String[] upload = FastDFSUtils.upload(file);
       String url= FastDFSUtils.getTrackerUrl()+upload[0]+"/"+upload[1];
       return adminService.updateAdminuserFace(url,id,authentication);
    }

}
